import redis
import urllib.request as ur
import time

class ProxyPool():
    def __init__(self):
        self.redis_conn=redis.StricRedis(
            host='localhost',
            port=6379,
            decode_responsed=True,
        )

    def set_proxy(self):
        proxy_odd = None
        while True:
            proxy_new = ur.urlopen('代理IP地址').read().decode('utf-8').strip().split(' ')
            if proxy_new !=proxy_odd:
                proxy_odd = proxy_new
                self.redis_conn.delete('proxy')
                self.redis_conn.sadd('proxy',*proxy_new)
                print("更换代理IP为：",proxy_new)
                time.sleep(1)
            else:
                time.sleep(2)

    def get_proxy(self):
        proxy_s = self.redis_conn.srandmember('proxy',1)
        if proxy_s:
            return  proxy_s[0]
        else:
            time.sleep(0.1)
            return self.get_proxy()


if __name__== '__main__':
    #ProxyPool().set_proxy()
    ProxyPool().get_proxy()